-- UIDeskClock
-- Created by huanghr July/26/2016
-- 座钟

UIDeskClock = class("UIDeskClock", function()
    return cc.Layer:create();
end);

function UIDeskClock.create(gridData)
    return UIDeskClock.new(gridData);
end

local initHour = 12;
local initMinite = 0;

-- 构造函数
function UIDeskClock:ctor(gridData)
    self:setName("UIDeskClock");

    -- 复用保险箱建筑界面
    local node = cc.CSLoader:createNode("layout/dungeon/special/DeskClock.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.gridData  = gridData;

    self.hour = initHour;
    self.minute = initMinite;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIDeskClock:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIDeskClock:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIDeskClock 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIDeskClock:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIDeskClock");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    -- 时钟拨动效果
    local btnNode = findChildByName(node, "CT/btn_node");
    local btnHour = btnNode:getChildByName("btn_hour");
    local function onHourClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            self.hour = (self.hour + 1) % 12;
            if self.hour == 0 then
                self.hour = 12;
            end

            if self.hour == self.gridData.hour then
                alert(getLocStr("deskclock_special_sound"));
            else
                alert(getLocStr("deskclock_time_change"));
            end

            self:updateDesc();
        end
    end
    AddTouchEventListener(btnHour, onHourClick);
    TextStyleM.setTextStyle(btnHour, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnHour:setTitleText(buildingInfo["view_text"]);

    -- 分钟拨动效果
    local btnMinute = btnNode:getChildByName("btn_minute");
    local function onMinuteClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            --
            self.minute = (self.minute + 5) % 60;

            if self.minute == self.gridData.minute then
                alert(getLocStr("deskclock_special_sound"));
            else
                alert(getLocStr("deskclock_time_change"));
            end

            self:updateDesc();
        end
    end
    AddTouchEventListener(btnMinute, onMinuteClick);
    TextStyleM.setTextStyle(btnMinute, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnMinute:setTitleText(buildingInfo["view_text2"]);

    -- 开门效果
    local btnOpen = btnNode:getChildByName("btn_open");
    local function onOpenClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            DungeonActionM.go("open_deskclock", self.gridData:getPos(),
                    bit.blshift(self.hour, 16) + self.minute);

            UIMgr.getCurrentScene():removeFormByName("UIDeskClock");
        end
    end
    AddTouchEventListener(btnOpen, onOpenClick);
    TextStyleM.setTextStyle(btnOpen, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOpen:setTitleText(getLocStr("open_door"));
end

-- 重绘
function UIDeskClock:redraw()
    -- 查询建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");
    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    titleLabel:setString(buildingInfo["title"]);
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    iconImg:loadTexture(iconPath);
    local iconSize = iconImg:getContentSize();

    iconImg:setScale(1.3);
    TextStyleM.setArtWordTitleStyle(titleLabel);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);

    self.dialog = buildingInfo["dialog"];

    applyString(descLabel, self.dialog);

    self:updateDesc();
end

-- 更新时间
function UIDeskClock:updateDesc()
    local hour = string.format("%02d", self.hour);
    local minute = string.format("%02d", self.minute);

    local dialogStr = string.format(getLocStr("deskclock_cur_time"), hour, minute);
    local timeLabel = findChildByName(self.node, "CT/time_label");

    TextStyleM.setTextStyle(timeLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE, false);

    timeLabel:setString(dialogStr);
end